package io.renren.modules.newBackground.shanDongCmcc.config;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;


/**
 * @author yun lang
 * @version 1.0
 * @description: TODO
 * @date 2024/11/18 11:04
 */
public class ShanDongCmccRSA {
    public static String sign(String dataStr, String privateKey) {
        try {
            byte[] data = dataStr.getBytes();
            byte[] keyBytes = (new BASE64Decoder()).decodeBuffer(privateKey);
            PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
            Signature signature = Signature.getInstance("MD5withRSA");
            signature.initSign(priKey);
            signature.update(data);
            return (new BASE64Encoder()).encodeBuffer(signature.sign());
        } catch (Exception e) {
            return null;
        }
    }
}
